package servlet;

import database.MySQLUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/checkMobile")
public class MyServlet_CheckMobile extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPOST...checkMobile");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String sql = "select * from user where mobile=" + req.getParameter("mobile");
        ResultSet resultSet = null;
        try {
            resultSet = MySQLUtils.getResultSet(sql);

            if (resultSet.next()) {
                //说明这个手机号已经存在
                out.print("true");
                System.out.println(true);
            } else {
                out.print("false");
                System.out.println(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}